package cn.cdg.mall.service.impl;

import cn.cdg.mall.mapper.UserMapper;
import cn.cdg.mall.pojo.po.UserPO;
import cn.cdg.mall.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserPO> selectAllUsers() {
        List<UserPO> userPOs = userMapper.selectList(null);
        return userPOs;
    }

    @Override
    public UserPO selectOneByUserNo(String user_no) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_no", user_no);
        List<UserPO> userPOs = userMapper.selectList(queryWrapper);

        return userPOs.isEmpty() ? null : userPOs.get(0);
    }

    @Override
    public boolean userLogin(UserPO user) {
        UserPO result = selectOneByUserNo(user.getUserNo());
        return result != null && Objects.equals(result.getPassword(), user.getPassword());
    }

    @Override
    public boolean userRegister(UserPO user) {
        UserPO result = selectOneByUserNo(user.getUserNo());
        if (result == null) {
            userMapper.insert(user);
            return true;
        }
        return false;
    }

}
